<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  
<div id="app">
  <p>2021年8月9日10:23:06</p>
  <cpn :cmessage="message" @itemclick="cpnclick"></cpn>
</div>

<template id="cpn">
  <div>
    <p>{{cmessage}}</p>
    <button v-for="item in categories" @click="btnClick(item)">{{item.name}}</button>
  </div>
</template>

  <script src="../vue/vue.js"></script>
  <script>
    const cpn = {
      template:'#cpn',
      props:{
        cmessage:String,
      },
      data(){
        return {
          categories:[
            {
              id:'001',
              name:'分类1'
            },
            {
              id:'002',
              name:'分类2'
            },
            {
              id:'003',
              name:'分类3'
            },
          ]
        }
      },
      methods:{
        btnClick(item){
          console.log('子组件打印的item：',item)
          //发射事件
          this.$emit('itemclick',item)
        }
      }

    }    
    


    const app = new Vue({
      el:'#app',
      data:{
        message:'hello'
      },
      components:{
        cpn
      },
      methods:{
        cpnclick(item){
          console.log('子传父成功，父组件打印的item：',item)
        }
      }
    })
  </script>
</body>
</html>